package com.linkage.educloud.ah.view.activity.contact;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.CallAdapter;
import com.linkage.educloud.ah.bean.ClazzContact;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.CommonPresenter;
import com.linkage.educloud.ah.mvp.ICommonContract;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.zhouyou.http.model.HttpParams;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;

/**
 * <desc>
 * Created by The Moss on 2018/11/26.
 */

public class ContactDetailActivity extends BasicTitleActivity implements ICommonContract.IView{
    @BindView(R.id.user_img)
    CircleImageView mUserAvatarIv;
    @BindView(R.id.name)
    TextView mNameTv;
    @BindView(R.id.role)
    TextView mRoleTv;
    @BindView(R.id.layout_user)
    LinearLayout mUserLl;
    @BindView(R.id.list)
    ListView mListView;

    static String KEY_CONTACT = "key_contact";

    ClazzContact clazzContact;
    CallAdapter mAdapter;

    ICommonContract.IPresenter iPresenter;
    public static void luncher(Context context, ClazzContact contact) {
        Intent intent = new Intent(context, ContactDetailActivity.class);
        intent.putExtra(KEY_CONTACT, contact);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_contacts_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getExtra();
        mAdapter = new CallAdapter(ContactDetailActivity.this);
        mAdapter.setListener(new CallAdapter.OnCheckOpenPhoneListener() {
            @Override
            public void onCheck(boolean ischeck) {
                HttpParams params = RequestUtil.openPhone(clazzContact.getClassid(),ischeck?1:0);
                iPresenter.request(params);
            }
        });
        mListView.setAdapter(mAdapter);
        loadLocalDatas();
        iPresenter = new CommonPresenter(this);
    }

    @OnClick(R.id.layout_user)
    public void onClick() {
        //跳转个人信息页面
    }

    @Override
    public void onSuccessResult(Object data) {

    }

    @Override
    public void onFailResult(String msg) {

    }

    private void getExtra() {
        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            return;
        }
        clazzContact = bundle.getParcelable(KEY_CONTACT);
        if (clazzContact == null) {
            return;
        }
        setTitle("我");
        mNameTv.setText(clazzContact.getName());
        if (clazzContact.getType() == Constants.UserType.PARENT) {
            mRoleTv.setText("家长");
        } else {
            mRoleTv.setText("老师");
        }
        ImageLoader.loadAvatar(this, clazzContact.getAvatar_url(), mUserAvatarIv);
    }

    private void loadLocalDatas() {
        Observable.create(new ObservableOnSubscribe<List<ClazzContact>>() {
            @Override
            public void subscribe(ObservableEmitter<List<ClazzContact>> e) throws Exception {
                try {
                    List<ClazzContact> contacts = MyApplication.getInstance().getResovler().getIMContactsForInvite(
                            AccountConfig.getLoginAccount().getLoginAccount(), "" + AccountConfig.getLoginAccount().getUserId());
                    if (contacts != null) {
                        e.onNext(contacts);
                    }else{
                        e.onError(new Throwable("contacts is null"));
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    e.onError(new Throwable("db cursor is error"));
                }
            }
        }).subscribe(new Consumer<List<ClazzContact>>() {
            @Override
            public void accept(List<ClazzContact> s) throws Exception {
                mAdapter.setDatas(s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }
}
